package com.goods.business.converter;

import com.goods.business.service.BusinessService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "物资类别")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private BusinessService businessService;

//   /business/productCategory/categoryTree?pageNum=1&pageSize=5
    @ApiOperation("物资类别列表展示")
    @GetMapping("/categoryTree")
    public ResponseBean categoryTree(Integer pageNum, Integer pageSize){
        List<ProductCategoryTreeNodeVO> productCategoryList=  businessService.categoryTree();
        Map<Object, Object> map = new HashMap<>();
        map.put("totle",productCategoryList.size());
        if(pageNum != null && pageSize != null){
            List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(productCategoryList, pageSize, pageNum);
            map.put("rows",page);
            return ResponseBean.success(map);
        }else {
            map.put("rows",productCategoryList);
            return ResponseBean.success(map);
        }
    }


    /*添加分类
    get   "business/productCategory/getParentCategoryTree"
            business/productCategory/getParentCategoryTree
    */
    @ApiOperation("修改分类时回显")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOList = businessService.getParentCategoryTree();
        return ResponseBean.success(ProductCategoryTreeNodeVOList);
    }

    /*: add responseBody*/
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory){
        boolean add = businessService.add(productCategory);
        if(add) {
            return ResponseBean.success();
        }else{
            return ResponseBean.error("新增失败");
        }
    }


    /*编辑分类(回显)
    get("business/productCategory/edit/" + id);*/
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategory productCategory = businessService.edit(id);
        return ResponseBean.success(productCategory);
    }

    /*编辑分类,"business/productCategory/update/"   put         /business/productCategory/update/20*/
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductCategory productCategory){
        businessService.update(productCategory);
        return ResponseBean.success();
    }

    //删除分类 business/productCategory/delete/" + id
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        Boolean delete = businessService.delete(id);
        if (delete){
            return ResponseBean.success();
        }
        else {
            Map<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("errorMsg", "此分类下还有子分类");
            return ResponseBean.error(objectObjectHashMap);
        }
    }



}
